﻿
Namespace std
    Public Class fixed_stack_wrapper(Of T, _MAX_SIZE As _int64)
        Private s As fixed_stack(Of T, _MAX_SIZE)

        Public Function back() As T
            Return s.back()
        End Function

        Public Sub pop()
            s.pop()
        End Sub

        Public Sub push(ByVal i As T)
            s.push(i)
        End Sub

        Public Function empty() As Boolean
            Return s.empty()
        End Function

        Public Function full() As Boolean
            Return s.full()
        End Function

        Public Sub clear()
            s.clear()
        End Sub
    End Class

    Public Structure fixed_stack(Of T, _MAX_SIZE As _int64)
        Private Shared ReadOnly MAX_SIZE As Int64 = 0
        Private index As Int64
        Private q() As T

        Shared Sub New()
            MAX_SIZE = +(alloc(Of _MAX_SIZE)())
            assert([default](Of T())() Is Nothing)
            assert([default](Of Int64)() = 0)
        End Sub

        Private Sub create()
            If q Is Nothing Then
                ReDim q(MAX_SIZE - 1)
                assert(Not q Is Nothing)
            End If
        End Sub

        Public Function back() As T
            assert(Not empty())
            Dim r As T = Nothing
            r = q(index - 1)
            Return r
        End Function

        Public Sub pop()
            assert(Not empty())
            q(index) = Nothing
            index -= 1
        End Sub

        Public Sub push(ByVal i As T)
            create()
            assert(Not full())
            q(index) = i
            index += 1
        End Sub

        Private Function index_is(ByVal expected As Int64) As Boolean
            Return index = expected
        End Function

        Public Function empty() As Boolean
            Return index_is(0)
        End Function

        Public Function full() As Boolean
            Return index_is(MAX_SIZE)
        End Function

        Public Sub clear()
            index = 0
            memclr(q)
        End Sub
    End Structure
End Namespace
